package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入类名");
        String className = sc.nextLine();
        System.out.println("请输入方法名");
        String methodName = sc.nextLine();

        try {
            Class cls = Class.forName(className);
            Constructor constructor = cls.getConstructor(String.class);
            Object obj = constructor.newInstance("张三");

            Method method1 = cls.getMethod("say",String.class);
            method1.invoke(obj,"大家好");
            System.out.println("-----------------------");

            Method method2 = cls.getMethod("say",String.class, int.class);
            method2.invoke(obj,"大家好",5);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
